package com.yuexun.dhdoctor.module.meetingNew.ui.fragment;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;


import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.BaseEntity;
import com.yuexun.dhdoctor.baselistener.ISearchCallBack;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.meetingNew.adapter.MeetingNewGetListAdapter;
import com.yuexun.dhdoctor.module.meetingNew.bean.MeetingNewDateBean;
import com.yuexun.dhdoctor.module.meetingNew.model.impl.MeetingNewModel;
import com.yuexun.dhdoctor.module.meetingNew.presenter.impl.MeetingNewPresenter;
import com.yuexun.dhdoctor.module.meetingNew.ui.Act_MeetingNew_Detail;
import com.yuexun.dhdoctor.module.meetingNew.ui.Act_MeetingNew_Main;
import com.yuexun.dhdoctor.module.meetingNew.ui.view.IMeetingNewView;
import com.yuexun.dhdoctor.util.DateUtil;
import com.yuexun.dhdoctor.widget.EmptyLayout;
import com.yuexun.dhdoctor.widget.PullToRefresh.FooterLoadingLayout;
import com.yuexun.dhdoctor.widget.PullToRefresh.PullToRefreshBase;
import com.yuexun.dhdoctor.widget.PullToRefresh.PullToRefreshList;

import org.kymjs.kjframe.ui.KJFragment;
import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by wangt on 2017/12/18.
 */

public class Frag_MeetingNew_Get extends KJFragment implements ISearchCallBack, IMeetingNewView {

    @Bind(R.id.patient_list)
    PullToRefreshList patientList;
    @Bind(R.id.empty_layout)
    EmptyLayout emptyLayout;

    private MeetingNewGetListAdapter adapter;
    private MeetingNewPresenter presenter;
    private List<MeetingNewDateBean> datas = new ArrayList<>();

    //搜索关键字
    private String startDate = "";
    private String endDate = "";

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        View mMainView = inflater.inflate(R.layout.frag_things_list, container, false);
        ButterKnife.bind(this, mMainView);
        return mMainView;
    }

    @Override
    protected void initData() {
        super.initData();
        presenter = new MeetingNewPresenter(new MeetingNewModel(), this);
        listViewPreference();

        startDate = DateFormat.format("yyyy-MM-dd", DateUtil.getFirstDayOfWeek(((Act_MeetingNew_Main) getActivity()).nowMeetingWeek)).toString();
        endDate = DateFormat.format("yyyy-MM-dd", DateUtil.getLastDayOfWeek(((Act_MeetingNew_Main) getActivity()).nowMeetingWeek)).toString();

        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
                ApiForGetList(startDate, endDate);
            }
        });
    }

    /**
     * 初始化ListView
     */
    private void listViewPreference() {
        ListView mList = patientList.getRefreshView();
        mList.setDivider(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
        mList.setOverscrollFooter(null);
        mList.setOverscrollHeader(null);
        mList.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
        patientList.setPullRefreshEnabled(false);
        patientList.setPullLoadEnabled(false);
        ((FooterLoadingLayout) patientList.getFooterLoadingLayout()).setNoMoreDataText("无更多数据");

        adapter = new MeetingNewGetListAdapter(mList, datas, R.layout.item_meetingnew);
        mList.setAdapter(adapter);

        patientList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
            }
        });

        mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                Intent intent = new Intent(getActivity(), Act_MeetingNew_Detail.class);
                bundle.putString("meetingId", datas.get(position).getMeetingId());
                bundle.putInt("type", 0);
                bundle.putInt("TAG", 1);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    /**
     * 网络请求获取列表数据
     */
    private void ApiForGetList(String startDate, String endDate) {
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuid", PreferenceHelper.readString(getActivity(), AppConfig.USER_INFO, AppConfig.SESSION_ID));
        params.put("startDate", startDate);
        params.put("endDate", endDate);
        params.put("launchFlag", "2");
        params.put("accountFlag", "true");
        presenter.getMeeting(params);
    }

    @Override
    public void searchBack(CharSequence s) {
        String[] strArr = s.toString().split("/");
        startDate = strArr[0];
        endDate = strArr[1];
        ApiForGetList(startDate, endDate);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        ApiForGetList(startDate, endDate);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            startDate = DateFormat.format("yyyy-MM-dd", DateUtil.getFirstDayOfWeek(((Act_MeetingNew_Main) getActivity()).nowMeetingWeek)).toString();
            endDate = DateFormat.format("yyyy-MM-dd", DateUtil.getLastDayOfWeek(((Act_MeetingNew_Main) getActivity()).nowMeetingWeek)).toString();
            ApiForGetList(startDate, endDate);
        }
    }

    @Override
    public void showMeetingList(BaseEntity<MeetingNewDateBean> bean) {
        if (bean.getStatus().equals("200")) {
            if (bean.getDatas().size() == 0) {
                patientList.setHasMoreData(false);
            } else {
                if (patientList.isPullLoadEnabled()) {
                    patientList.onPullUpRefreshComplete();
                }
            }
            datas = bean.getDatas();
            adapter.refresh(datas);
        } else {
            ViewInject.toast(bean.getMessage());
        }

        if (datas.size() == 0) {
            emptyLayout.setErrorType(EmptyLayout.NODATA);
        } else {
            emptyLayout.dismiss();
        }
    }
}
